goog.provide('ee.arrow');

goog.require('lime.Sprite');
goog.require('lime.animation.MoveTo');


this.dir =  "left";
this.belong = null;
this.target = null;

ee.Arrow=  function(direction){
    lime.Sprite.call(this);

    this.dir = direction;
    this.setSize(80,100).setFill("assets/arrow.png");

    switch(direction){
        case "left":
            this.setRotation(90);
            break;
        case "right":
            this.setRotation(-90);
            break;
        case "top":
            this.setRotation(0);
            break;
        case "bottom":
            this.setRotation(180);
            break;
    }
};

goog.inherits(ee.Arrow,lime.Sprite);

ee.Arrow.prototype.showArrow = function(belong,target){
    if(this.getHidden()){
        this.belong = belong;
        this.target = target;
        var x = this.belong.getPosition().x;
        var y = this.belong.getPosition().y;
        switch(this.dir){
            case "left":
                this.setPosition(x-80,y);
                break;
            case "right":
                this.setPosition(x+80,y);
                break;
            case "top":
                this.setPosition(x,y-80);
                break;
            case "bottom":
                this.setPosition(x,y+80);
                break;
        }
        this.setHidden(false);
        goog.events.listen(this,"touchend",this.onclick);
    }
};
ee.Arrow.prototype.onclick = function(e){
    helloworld.removeArrow();
    if(this.target.side == "C"){
        this.justmove();
        return;
    }
    if(this.target.side == this.belong.side){
        if(this.target.die){
            this.justmove();
        }
    }else{
        if(this.target.die){
            this.justmove();
        }else{
            this.eat(); 
        }
    }
};
ee.Arrow.prototype.justmove = function(){
    var exchange_i = this.target.posi;
    var exchange_j = this.target.posj;
    var exchange_pos = this.target.getPosition();
    this.target.posi = this.belong.posi;
    this.target.posj = this.belong.posj;
    this.belong.posi = exchange_i;
    this.belong.posj = exchange_j;
    this.target.runAction(new lime.animation.MoveTo(this.belong.getPosition()).setDuration(0.2));
    this.belong.runAction(new lime.animation.MoveTo(exchange_pos).setDuration(0.2));
};
ee.Arrow.prototype.eat = function(){
    this.target.die = true;
    this.target.setHidden(true);
    this.justmove();
};
ee.Arrow.prototype.hideArrow = function(){
    this.setHidden(true);
    goog.events.unlisten(this,'touchend',this.onclick);
};
ee.Arrow.prototype.id = "arrow"; 
goog.exportSymbol('ee.Arrow', ee.Arrow);
